getActiveSheet();
// Set headers
$headers = [
'A1' => 'Question Number',
'B1' => 'Question Text',
'C1' => 'Option A',
'D1' => 'Option B',
'E1' => 'Option C',
'F1' => 'Option D',
'G1' => 'Correct Answer',
'H1' => 'Marks'
];
foreach ($headers as $cell => $value) {
$sheet->setCellValue($cell, $value);
}
// Add sample questions
$sampleQuestions = [
[
'What is the capital of France?',
'London',
'Paris',
'Berlin',
'Madrid',
'B',
1
],
[
'What is 5 × 7?',
'30',
'35',
'40',
'45',
'B',
1
],
[
'Which planet is known as the Red Planet?',
'Earth',
'Mars',
'Jupiter',
'Saturn',
'B',
1
]
];
$row = 2;
foreach ($sampleQuestions as $index => $question) {
$sheet->setCellValue('A' . $row, $index + 1);
$sheet->setCellValue('B' . $row, $question[0]);
$sheet->setCellValue('C' . $row, $question[1]);
$sheet->setCellValue('D' . $row, $question[2]);
$sheet->setCellValue('E' . $row, $question[3]);
$sheet->setCellValue('F' . $row, $question[4]);
$sheet->setCellValue('G' . $row, $question[5]);
$sheet->setCellValue('H' . $row, $question[6]);
$row++;
}
// Style the headers
$headerStyle = [
'font' => [
'bold' => true,
'color' => ['rgb' => 'FFFFFF']
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => ['rgb' => '34495E']
]
];
$sheet->getStyle('A1:H1')->applyFromArray($headerStyle);
// Auto-size columns
foreach (range('A', 'H') as $column) {
$sheet->getColumnDimension($column)->setAutoSize(true);
}
// Set borders
$sheet->getStyle('A1:H' . ($row-1))->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
// Create writer and output
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="questions_template.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
exit;
} catch (Exception $e) {
$_SESSION['error'] = "Error generating Excel template: " . $e->getMessage();
header('Location: download_template.php');
exit;
}
}
function downloadCSVTemplate() {
try {
// Set headers for CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="questions_template.csv"');
// Create output stream
$output = fopen('php://output', 'w');
// Add BOM for UTF-8 to help with Excel compatibility
fputs($output, "\xEF\xBB\xBF");
// Write headers
fputcsv($output, [
'Question Number',
'Question Text',
'Option A',
'Option B',
'Option C',
'Option D',
'Correct Answer',
'Marks'
]);
// Write sample questions
$sampleQuestions = [
[
'1',
'What is the capital of France?',
'London',
'Paris',
'Berlin',
'Madrid',
'B',
'1'
],
[
'2',
'What is 5 × 7?',
'30',
'35',
'40',
'45',
'B',
'1'
],
[
'3',
'Which planet is known as the Red Planet?',
'Earth',
'Mars',
'Jupiter',
'Saturn',
'B',
'1'
]
];
foreach ($sampleQuestions as $question) {
fputcsv($output, $question);
}
fclose($output);
exit;
} catch (Exception $e) {
$_SESSION['error'] = "Error generating CSV template: " . $e->getMessage();
header('Location: download_template.php');
exit;
}
}
?>
Download Template - Admin
← Back to Upload Questions
Template Format
Your template file will contain the following columns:
💡 Tips for Success:
- Start entering your questions from row 2
- Do not modify or delete the header row (row 1)
- Ensure correct answers are A, B, C, or D (uppercase)
- All options (A, B, C, D) must be filled for each question
- Question text is required for each row
- Save your file before uploading